package com.han.practice;

import java.lang.reflect.*;

interface PaymentService {
    void pay(double amount);
}

class AlipayService implements PaymentService {
    public void pay(double amount) {
        System.out.println("支付宝支付: " + amount + " 元");
    }
}

public class ProxyDemo01 {
    public static void main(String[] args) {
        PaymentService target = new AlipayService();

/*        PaymentService proxy = (PaymentService) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("【AOP】开始支付...");
                        Object result = method.invoke(target, args);
                        System.out.println("【AOP】支付完成！");
                        return result;
                    }
                }
        );*/
        PaymentService proxy = (PaymentService) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                (proxy1, method, args1) -> {
                    System.out.println("【AOP】开始支付...");
                    Object result = method.invoke(target, args1);
                    System.out.println("【AOP】支付完成！");
                    return result;
                }
        );

        proxy.pay(99.9);
    }
}